package com.kapo.egroup.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kapo.egroup.entity.base.project.Project;
import com.kapo.egroup.entity.base.project.UserApplyRecord;
import com.kapo.egroup.entity.base.project.UserInviteRecord;
import com.kapo.egroup.entity.base.project.UserProjectRecord;
import com.kapo.egroup.mapper.ProjectMapper;
import com.kapo.egroup.mapper.UserInviteRecordMapper;
import com.kapo.egroup.mapper.UserProjectRecordMapper;
import com.kapo.egroup.service.InviteService;
import com.kapo.egroup.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Title InviteServiceImpl
 * @Description TODO
 * @Author 86159
 * @Date 2023-04-01 00:24
 **/
@Service
public class InviteServiceImpl extends ServiceImpl<UserInviteRecordMapper, UserInviteRecord> implements InviteService {
    @Autowired
    UserInviteRecordMapper userInviteRecordMapper;

    @Autowired
    UserProjectRecordMapper userProjectRecordMapper;

    @Override
    public void inviteProjectByUserId(Long projectId, Long receiverId, Long senderId,String description,String outline) {
        UserInviteRecord userInviteRecord = new UserInviteRecord();
        userInviteRecord.setProjectId(projectId);
        userInviteRecord.setSenderId(senderId);
        userInviteRecord.setReceiverId(receiverId);
        userInviteRecord.setDescription(description);
        userInviteRecord.setState(0);
        userInviteRecord.setWechatExchangeState(0);
        userInviteRecord.setOutline(outline);
        baseMapper.insert(userInviteRecord);
    }

    @Override
    public void acceptInviteById(Long inviteRecordId) {
        UserInviteRecord userInviteRecord = userInviteRecordMapper.selectById(inviteRecordId);
        userInviteRecord.setState(1);
        QueryWrapper<UserInviteRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("_id",inviteRecordId);
        baseMapper.update(userInviteRecord,queryWrapper);
        //  添加申请人到项目组中
        Long applicantId = userInviteRecord.getReceiverId();
        UserProjectRecord userProjectRecord = new UserProjectRecord();
        userProjectRecord.setUserId(applicantId);
        userProjectRecord.setProjectId(userInviteRecord.getProjectId());
        userProjectRecordMapper.insert(userProjectRecord);
    }

    @Override
    public void rejectInviteById(Long inviteRecordId) {
        UserInviteRecord userInviteRecord = userInviteRecordMapper.selectById(inviteRecordId);
        userInviteRecord.setState(-1);
        QueryWrapper<UserInviteRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("_id",inviteRecordId);
        baseMapper.update(userInviteRecord,queryWrapper);
    }

    @Override
    public List<UserInviteRecord> getInviteByUserId(Long userId) {
        QueryWrapper<UserInviteRecord> queryWrapperReceiver = new QueryWrapper<>();
        QueryWrapper<UserInviteRecord> queryWrapperSender = new QueryWrapper<>();
        queryWrapperReceiver.eq("receiver_id",userId);
        queryWrapperSender.eq("sender_id",userId);
        List<UserInviteRecord> userInviteRecordReceiverList = baseMapper.selectList(queryWrapperReceiver);
        List<UserInviteRecord> userInviteRecordSenderList = baseMapper.selectList(queryWrapperSender);
        List<UserInviteRecord> resList = new ArrayList<>(userInviteRecordReceiverList);
        resList.addAll(userInviteRecordSenderList);
        return resList;
    }
}
